﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// ERP销售订单信息查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ERPSaleOrderInfoController : ControllerBase
    {
        private readonly ILogger<ERPSaleOrderInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public ERPSaleOrderInfoController(ILogger<ERPSaleOrderInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("ERP_DbConnection");

        }

        /// <summary>
        /// ERP销售订单信息查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ERPSaleOrderSelect([FromBody] ERPSaleOrderInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            ERPSaleOrderInfoRequestDto result = new ERPSaleOrderInfoRequestDto();
            ERPSaleOrderInfoResponseDto re = new ERPSaleOrderInfoResponseDto();
            string ORGANIZATION_CODE = strparm.ORGANIZATION_CODE;
            string ORDER_NUMBER = strparm.ORDER_NUMBER;
            string row = strparm.row;



            try
            {
                if (ORDER_NUMBER.Trim() != "")
                {
                    strSql = new StringBuilder();
                    strSql.Append(@"select ORGANIZATION_CODE,HEADER_ID,ORDER_NUMBER,BOOKED_DATE,CUST_PO_NUMBER,CUSTOMER_NUMBER,CUSTOMER_NAME,SALESREP_NAME,ORDER_TYPE,HEADER_CONTACT_NAME CONTACT_NAME,HEADER_PHONE_NUMBER PHONE_NUMBER,SHIP_ADDRESS1,LINE_NUMBER,
LINE_ID,ITEM_NUMBER,ITEM_DESC,UOM,ORDERED_QUANTITY,SHIPPED_QUANTITY,LINE_STATUS,CONCAT('F',SHIP_LOCATION) SHIP_LOCATION  from  apps.CUX_OM_ORDER_DETAIL_V  WHERE 1=1 and ORDER_NUMBER='" + ORDER_NUMBER + "' ");
                }
                else
                {
                    strSql = new StringBuilder();
                    strSql.Append(@"select ORGANIZATION_CODE,HEADER_ID,ORDER_NUMBER,BOOKED_DATE,CUST_PO_NUMBER,CUSTOMER_NUMBER,CUSTOMER_NAME,SALESREP_NAME,ORDER_TYPE,HEADER_CONTACT_NAME CONTACT_NAME,HEADER_PHONE_NUMBER PHONE_NUMBER,SHIP_ADDRESS1,LINE_NUMBER,
LINE_ID,ITEM_NUMBER,ITEM_DESC,UOM,ORDERED_QUANTITY,SHIPPED_QUANTITY,LINE_STATUS,CONCAT('F',SHIP_LOCATION) SHIP_LOCATION  from  apps.CUX_OM_ORDER_DETAIL_V  WHERE 1=1 and  BOOKED_DATE  between to_char(sysdate-30,'yyyy-mm-dd hh24:mi:ss') and to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')");
                }
                if (ORGANIZATION_CODE.Trim()!="")
                {
                    strSql.Append(" and ORGANIZATION_CODE='" + ORGANIZATION_CODE.Trim() + "'");
                }

                if (row.Trim() != "")
                {
                    strSql.Append(" and rownum<=" + row.Trim() + "");
                }


                DataTable dt_barcodecheck = DB.OracleQuery(strSql.ToString(), con);
                if (dt_barcodecheck.Rows.Count<=0)
                {
                    strMsg = "无数据，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel<ERPSaleOrderInfoResponseData>(dt_barcodecheck);
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
